﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using VMultiAPI;

namespace ZSens
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private Zsens zsens;
        private Network network;
        private ViewModel.MainWindowViewModel viewModel;
        private static MainWindow ui;
        private Help help;

        public static MainWindow getInstance()
        {
            return ui;
        }

        public MainWindow()
        {
            int numPlayer = 2;

            if (VMulti.GetInstance() == null)
            {
                MessageBox.Show("VMulti device not connected");
                Application.Current.Shutdown();
            }

            zsens = new Zsens(numPlayer, this);
            network = new Network(numPlayer, zsens);

            InitializeComponent();
            this.viewModel = new ViewModel.MainWindowViewModel(this);
            this.DataContext = viewModel;
            updateNetworkThread = new System.Threading.Thread(UpdateNetwork);
            updateVMultiThread = new System.Threading.Thread(UpdateVMultiThread);
            ui = this;
            updateVMultiThread.Start();
            updateNetworkThread.Start();

            help = new Help();
            help.Show();
            help.Activate();
        }

        public void UpdateVMultiThread()
        {
            while (true)
            {
                for (int i = 0; i < zsens.players.Count; i++)
                {
                    Player player = zsens.players[i];
                    if (player.status == Player.Running)
                    {
                        if (player.mapStatus)
                        {
                            if (player.mapStatus)
                            {
                                player.UpdateAction();
                                player.mapStatusOld = true;
                            }
                            else
                            {
                                if (player.mapStatusOld)
                                {
                                    VMulti.GetInstance().Reset();
                                    player.mapStatusOld = false;
                                }
                            }
                        }
                        else
                        {
                            if (player.mapStatusOld)
                            {
                                VMulti.GetInstance().Reset();
                                player.mapStatusOld = false;
                            }
                        }
                    }
                    else if (player.status == Player.Recycling)
                    {
                        player.status = Player.Destructing;
                        VMulti.GetInstance().Reset();
                        zsens.players.RemoveAt(i);
                        i--;
                    }
                }
                Thread.Sleep(10);
            }
            //wait for semaphore..
        }

        public void StopVMultiUpdate()
        {
            updateVMultiThread.Abort();
            updateVMultiThread.Join();
        }

        public void StartVMultiUpdate()
        {
            updateVMultiThread = new Thread(UpdateVMultiThread);
            updateVMultiThread.Start();
        }

        public void UpdateNetwork()
        {
            network.Run();
        }

        public void AddPlayer(Player player)
        {
            this.Dispatcher.Invoke(new Action<Player>(viewModel.AddPlayer), new object[] { player });
        }

        public void RemovePlayer(Player player)
        {
            this.Dispatcher.Invoke(new Action<Player>(viewModel.RemovePlayer), new object[] { player });
        }

        public bool IsBegin()
        {
            bool result = viewModel.IsBegin;
            return result;
        }

        public void SetBegin(bool isBegin)
        {
            viewModel.IsBegin = isBegin;
            return;
        }


        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            //VMulti.GetInstance().Joystick.Buttons[0].Pressed = true;
            VMulti.GetInstance().Joystick.HatPosition += (byte)(45.0 / 360.0 * 256);
            var k = VMulti.GetInstance().Keyboard.GetKey(VMultiAPI.Keyboard.KeyCode.KeyLeftWindows);
            //k.Pressed = !k.Pressed;
            k.Press();
            Console.Write((int)VMultiAPI.Keyboard.KeyCode.KeyNumDot);
        }

        private System.Threading.Thread updateVMultiThread;
        private System.Threading.Thread updateNetworkThread;

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            //network thread end...
            network.Stop();
            updateVMultiThread.Abort();
            if (VMulti.GetInstance() != null)
                VMulti.GetInstance().Reset();
        }


        private void BeginGame(object sender, RoutedEventArgs e)
        {
            SetBegin(true);
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            if (!help.IsLoaded)
            {
                help = new Help();
                help.Show();
            }
        }
    }
}
